<template>
  <el-dialog v-model="showAddForm" title="新建WG" width="30%">
    <el-form :model="appObject.form" label-width="120px" style="margin-top: 20px;" ref="formRef" :rules="rules">
      <el-form-item label="WG名字" prop="name">
        <el-input v-model="appObject.form.name"/>
      </el-form-item>
      <el-form-item label="WG描述" prop="description">
        <el-input v-model="appObject.form.description" type="textarea" />
      </el-form-item>
      <el-form-item label="WG日程" prop="schedule">
        <el-input v-model="appObject.form.schedule"/>
      </el-form-item>
      <el-form-item label="WG目录" prop="learnoutcome">
        <el-input v-model="appObject.form.learnoutcome"/>
      </el-form-item>
      <el-form-item label="WG开始日期" prop="startTime">
        <el-date-picker v-model="appObject.form.startTime" type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"/>
      </el-form-item>
      <el-form-item label="WG结束日期" prop="endTime">
        <el-date-picker v-model="appObject.form.endTime" type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"/>
      </el-form-item>
      <el-form-item>
        <el-button @click="showAddForm = false">取消</el-button>
        <el-button type="primary" @click="submitForm(formRef)">确认</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup>
  import { reactive, ref } from 'vue'
  import { ElMessage } from 'element-plus'
  
  import {storeToRefs} from 'pinia'
  import wgStore from '../store/wg.js'
  import userStore from '../store/user.js'
  const wStore = wgStore()
  const uStore = userStore()
  
  import {
    addWG
  } from '../http/wgApi.js'
  
  import emitter from '../mitt/index.js'
  
  const { showAddForm  } = storeToRefs(wStore)
  
  const appObject = reactive({
    form: {
      name: '',
      description: '',
      schedule: '',
      learnoutcome: '',
      creatorId: uStore.getUserId,
      startTime: "",
      endTime: ""
    }
  })
  
  const initData = () => {
    appObject.form = {
      name: '',
      description: '',
      schedule: '',
      learnoutcome: '',
      creatorId: uStore.getUserId
    }
  }
  
  
  // 表单校验
  const rules = reactive({
    name: [
      { required: true, message: '请输入WG名称', trigger: 'blur' },
    ],
    description: [
      { required: true, message: '请输入WG描述', trigger: 'blur' },
    ],
    schedule: [
      { required: true, message: '请输入WG日程', trigger: 'blur' },
    ],
    learnoutcome: [
      { required: true, message: '请输入WG目录', trigger: 'blur' },
    ]
  })
  
  const formRef = ref()
  
  const submitForm = (formEL) => {
    if (!formEL) return
    formEL.validate((valid) => {
      if (valid) {
        addWG(appObject.form).then(res => {
          res = res.data
          if(res.code == 200) {
            ElMessage({
              message: "WG创建成功",
              type: "success"
            })
            wStore.updateShowAddForm(false)
            initData()
            // 使用mitt注册一个事件，通知wg任务页面刷新
            emitter.emit("reloadWg", {})
          }else {
            ElMessage({
              message: res.msg,
              type: "error"
            })
          }
        })
      }else{
        return false
      }
    })
  }
  
  
  
</script>

<style scoped>
</style>
